#include "fxcc/core/platform/win32/CallBacks.h"
#include "fxcc/core/platform/win32/App.h"
#include <Xinput.h>

using namespace fxcc::platform;
using namespace fxcc::platform::win32;

win32::App* win32::CallBacks::m_CurrentApp = 0;


std::unordered_map<int, KeyCode> CallBacks::m_KeyMap =
{
   {0x1E, KeyCode::A}, // A
    {0x30, KeyCode::B}, // B
    {0x2E, KeyCode::C}, // C
    {0x20, KeyCode::D}, // D
    {0x12, KeyCode::E}, // E
    {0x21, KeyCode::F}, // F
    {0x22, KeyCode::G}, // G
    {0x23, KeyCode::H}, // H
    {0x17, KeyCode::I}, // I
    {0x24, KeyCode::J}, // J
    {0x25, KeyCode::K}, // K
    {0x26, KeyCode::L}, // L
    {0x32, KeyCode::M}, // M
    {0x31, KeyCode::N}, // N
    {0x18, KeyCode::O}, // O
    {0x19, KeyCode::P}, // P
    {0x10, KeyCode::Q}, // Q
    {0x13, KeyCode::R}, // R
    {0x1F, KeyCode::S}, // S
    {0x14, KeyCode::T}, // T
    {0x16, KeyCode::U}, // U
    {0x2F, KeyCode::V}, // V
    {0x11, KeyCode::W}, // W
    {0x2D, KeyCode::X}, // X
    {0x15, KeyCode::Y}, // Y
    {0x2C, KeyCode::Z}, // Z
    {0x0A, KeyCode::NUM0}, // 0
    {0x02, KeyCode::NUM1}, // 1
    {0x03, KeyCode::NUM2}, // 2
    {0x04, KeyCode::NUM3}, // 3
    {0x05, KeyCode::NUM4}, // 4
    {0x06, KeyCode::NUM5}, // 5
    {0x07, KeyCode::NUM6}, // 6
    {0x08, KeyCode::NUM7}, // 7
    {0x09, KeyCode::NUM8}, // 8
    {0x0B, KeyCode::NUM9}, // 9
    {0x01, KeyCode::ESC}, // ESC
    {0x1C, KeyCode::ENTER}, // ENTER
    {0x39, KeyCode::SPACE}, // SPACE
    {0x0E, KeyCode::BACKSPACE}, // BACKSPACE
    {0x48, KeyCode::UP}, // UP
    {0x4B, KeyCode::LEFT}, // LEFT
    {0x4D, KeyCode::RIGHT}, // RIGHT
    {0x50, KeyCode::DOWN}, // DOWN
    {0x38, KeyCode::LEFTALT}, // LEFT ALT
    {0x2A, KeyCode::LEFTSHIFT}, // LEFT SHIFT
    {0x1D, KeyCode::LEFTCTRL}, // LEFT CTRL
    {0xB8, KeyCode::RIGHTALT}, // RIGHT ALT
    {0x36, KeyCode::RIGHTSHIFT}, // RIGHT SHIFT
    {0x9D, KeyCode::RIGHTCTRL} // RIGHT CTRL
};

std::unordered_map<int, MouseButton> CallBacks::m_MouseMap =
{
    {1, MouseButton::LEFT},   
    {2, MouseButton::RIGHT},  
    {3, MouseButton::MIDDLE},
    {4, MouseButton::BUTTON1},
    {5, MouseButton::BUTTON2},
};

std::unordered_map<int, JoystickButton> CallBacks::m_JoystickMap =
{
    { XINPUT_GAMEPAD_START, JoystickButton::START},
    { XINPUT_GAMEPAD_BACK, JoystickButton::BACK },
    { XINPUT_GAMEPAD_LEFT_THUMB, JoystickButton::LSTICK },
    { XINPUT_GAMEPAD_RIGHT_THUMB, JoystickButton::RSTICK},
    { XINPUT_GAMEPAD_LEFT_SHOULDER, JoystickButton::LB },
    { XINPUT_GAMEPAD_RIGHT_SHOULDER, JoystickButton::RB},
    { XINPUT_GAMEPAD_A, JoystickButton::A },
    { XINPUT_GAMEPAD_B, JoystickButton::B},
    { XINPUT_GAMEPAD_X, JoystickButton::X},
    { XINPUT_GAMEPAD_Y, JoystickButton::Y},

};

void fxcc::platform::win32::CallBacks::Register(win32::App* app)
{
    m_CurrentApp = app;
}

LRESULT __stdcall fxcc::platform::win32::CallBacks::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    return m_CurrentApp->OnWndProj(hWnd, msg, wParam, lParam);
};